import requests
import pymysql
import json
from backend.db_set import db
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
db_data = db
urllist = [
    ]

def py(url_1):
    hea = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    # url = 'https://apis.j3pz.com/equip/'+str()
    url = url_1
    # 49297 60000
    html = requests.get(url, headers=hea,verify=False)
    html.encoding = 'utf-8'
    html = json.loads(html.text)
    # print(html)
    return html

import urllib
# a = '%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%8D%AF%E5%AE%97&MagicKind=%E6%A0%B9%E9%AA%A8,%E5%86%85%E5%8A%9F'
# print(urllib.request.unquote(a))
# b = '通用,精简,药宗&MagicKind=根骨,内功'
# print(urllib.request.quote(b))


def db_login():
    try:
        db = pymysql.connect(db_data['server'], db_data['user_name'], db_data['pwd'], "yantian", charset='utf8')
    except Exception as e:
        pass
    return db


def check_zhuangbei(url_1):
    js = py(url_1)
    k = js['list']
    for i in k :
        yuanqi = 0
        gongji = 0
        huixin = 0
        huixiao = 0
        pofang = 0
        wushuang = 0
        pozhao = 0
        jiasu = 0
        shuxing = [i['_Magic1Type'],i['_Magic2Type'],i['_Magic3Type'],i['_Magic4Type'],i['_Magic5Type'],i['_Magic6Type'],i['_Magic7Type'],i['_Magic8Type'],i['_Magic9Type'],i['_Magic10Type'],i['_Magic11Type'],i['_Magic12Type']]
        for m in shuxing:
            if m != None:
                if m['attr'][0] == 'atSpunkBase':
                    yuanqi = m['attr'][1]
                elif m['attr'][0] == 'atNeutralAttackPowerBase':
                    gongji = m['attr'][1]
                elif m['attr'][0] == 'atMagicAttackPowerBase':
                    gongji = m['attr'][1]
                elif m['attr'][0] == 'atAllTypeCriticalStrike':
                    huixin = m['attr'][1]
                elif m['attr'][0] == 'atMagicCriticalStrike':
                    huixin = m['attr'][1]
                elif m['attr'][0] == 'atNeutralCriticalStrike':
                    huixin = m['attr'][1]
                elif m['attr'][0] == 'atAllTypeCriticalDamagePowerBase':
                    huixiao = m['attr'][1]
                elif m['attr'][0] == 'atNeutralOvercomeBase':
                    pofang = m['attr'][1]
                elif m['attr'][0] == 'atMagicOvercome':
                    pofang = m['attr'][1]
                elif m['attr'][0] == 'atStrainBase':
                    wushuang = m['attr'][1]
                elif m['attr'][0] == 'atSurplusValueBase':
                    pozhao = m['attr'][1]
                elif m['attr'][0] == 'atHasteBase':
                    jiasu = m['attr'][1]
        if i['_SetAttrbs'] == {}:
            taozhuang = ''
        else:
            # print(i['_SetAttrbs'])
            taozhuang = i['_SetAttrbs']['Name']
        # print(i['_DiamondAttributeID1'])
        if i['_DiamondAttributeID1'] != None:
            if i['_DiamondAttributeID1'][0] == 'atMagicOvercome':
                kong1 = '破防'
            elif i['_DiamondAttributeID1'][0] == 'atNeutralOvercomeBase':
                kong1 = '破防'
            elif i['_DiamondAttributeID1'][0] == 'atSpunkBase':
                kong1 = '元气'
            elif i['_DiamondAttributeID1'][0] == 'atSurplusValueBase':
                kong1 = '破招'
            elif i['_DiamondAttributeID1'][0] == 'atMagicAttackPowerBase':
                kong1 = '攻击'
            elif i['_DiamondAttributeID1'][0] == 'atNeutralAttackPowerBase':
                kong1 = '攻击'
            elif i['_DiamondAttributeID1'][0] == 'atNeutralCriticalStrike':
                kong1 = '会心'
            elif i['_DiamondAttributeID1'][0] == 'atAllTypeCriticalStrike':
                kong1 = '会心'
            elif i['_DiamondAttributeID1'][0] == 'atMagicCriticalStrike':
                kong1 = '会心'
            elif i['_DiamondAttributeID1'][0] == 'atNeutralCriticalDamagePowerBase':
                kong1 = '会效'
            elif i['_DiamondAttributeID1'][0] == 'atAllTypeCriticalDamagePowerBase':
                kong1 = '会效'
            elif i['_DiamondAttributeID1'][0] == 'atStrainBase':
                kong1 = '无双'
        else:
            kong1 = ''

        
        if i['_DiamondAttributeID2'] != None:
            if i['_DiamondAttributeID2'][0] == 'atMagicOvercome':
                kong2 = '破防'
            elif i['_DiamondAttributeID2'][0] == 'atNeutralOvercomeBase':
                kong2 = '破防'
            elif i['_DiamondAttributeID2'][0] == 'atSpunkBase':
                kong2 = '元气'
            elif i['_DiamondAttributeID2'][0] == 'atSurplusValueBase':
                kong2 = '破招'
            elif i['_DiamondAttributeID2'][0] == 'atMagicAttackPowerBase':
                kong2 = '攻击'
            elif i['_DiamondAttributeID2'][0] == 'atNeutralAttackPowerBase':
                kong2 = '攻击'
            elif i['_DiamondAttributeID2'][0] == 'atNeutralCriticalStrike':
                kong2 = '会心'
            elif i['_DiamondAttributeID2'][0] == 'atAllTypeCriticalStrike':
                kong2 = '会心'
            elif i['_DiamondAttributeID2'][0] == 'atMagicCriticalStrike':
                kong2 = '会心'
            elif i['_DiamondAttributeID2'][0] == 'atNeutralCriticalDamagePowerBase':
                kong2 = '会效'
            elif i['_DiamondAttributeID2'][0] == 'atAllTypeCriticalDamagePowerBase':
                kong2 = '会效'
            elif i['_DiamondAttributeID2'][0] == 'atStrainBase':
                kong2 = '无双'
        else:
            kong2 = ''
        if i['_DiamondAttributeID3'] != None:
            if i['_DiamondAttributeID3'][0] == 'atMagicOvercome':
                kong3 = '破防'
            elif i['_DiamondAttributeID3'][0] == 'atNeutralOvercomeBase':
                kong3 = '破防'
            elif i['_DiamondAttributeID3'][0] == 'atSpunkBase':
                kong3 = '元气'
            elif i['_DiamondAttributeID3'][0] == 'atSurplusValueBase':
                kong3 = '破招'
            elif i['_DiamondAttributeID3'][0] == 'atMagicAttackPowerBase':
                kong3 = '攻击'
            elif i['_DiamondAttributeID3'][0] == 'atNeutralAttackPowerBase':
                kong3 = '攻击'
            elif i['_DiamondAttributeID3'][0] == 'atNeutralCriticalStrike':
                kong3 = '会心'
            elif i['_DiamondAttributeID3'][0] == 'atAllTypeCriticalStrike':
                kong3 = '会心'
            elif i['_DiamondAttributeID3'][0] == 'atMagicCriticalStrike':
                kong3 = '会心'
            elif i['_DiamondAttributeID3'][0] == 'atNeutralCriticalDamagePowerBase':
                kong3 = '会效'
            elif i['_DiamondAttributeID3'][0] == 'atAllTypeCriticalDamagePowerBase':
                kong3 = '会效'
            elif i['_DiamondAttributeID3'][0] == 'atStrainBase':
                kong3 = '无双'
        else:
            kong3 = ''
        # print(i['ID'])
        buwei = {
            '0': 'primaryWeapon',
            '1':'secondaryWeapon',
            '2': 'jacket',
            '3': 'hat',
            '4':'necklace',
            '5':'ring',
            '6': 'belt',
            '7': 'pendant',
            '8': 'bottom',
            '9': 'shoes',
            '10':'wrist',
        }

        ls = [i['ID'],i['Name'],buwei[i['SubType']],i['BelongSchool'],i['Level'],0,yuanqi,gongji,huixin,huixiao,pofang,wushuang,pozhao,jiasu,i['MaxStrengthLevel'],taozhuang,'',kong1,kong2,kong3,'']

        if ls[8] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '会心'
        if ls[9] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '会效'
        if ls[10] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '破防'
        if ls[11] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '无双'
        if ls[12] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '破招'
        if ls[13] != 0:
            if ls[-1] != '':
                ls[-1] += ','
            ls[-1] += '加速'
        db = db_login()
        cursor = db.cursor()
        sql = "insert into jx3_equipment(id,name,position,item,level,score,yuanqi,gongji,huixin,huixiao,pofang,wushuang,pozhao,jiasu,max_strengthen,ex,source,kong1,kong2,kong3,beizhu) VALUES (%s,'%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'%s','%s','%s','%s','%s','%s')"%(ls[0],ls[1],ls[2],ls[3],ls[4],ls[5],ls[6],ls[7],ls[8],ls[9],ls[10],ls[11],ls[12],ls[13],ls[14],ls[15],ls[16],ls[17],ls[18],ls[19],ls[20])
        cursor.execute(sql)
        db.commit()
        print(ls)
        cursor.close()
        db.close()

# for i in range(2,11):
#     urllist.append('https://node.jx3box.com/equip/armor?client=std&position='+str(i)+'&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=5500&max_level=6430&BelongSchool=%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%A1%8D%E5%A4%A9&MagicKind=%E5%85%83%E6%B0%94,%E5%86%85%E5%8A%9F')
urllist.append('https://node.jx3box.com/equip/trinket?client=std&position=4&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=5500&max_level=6430&BelongSchool=%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%A1%8D%E5%A4%A9&MagicKind=%E5%85%83%E6%B0%94,%E5%86%85%E5%8A%9F')
urllist.append('https://node.jx3box.com/equip/trinket?client=std&position=7&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=5500&max_level=6430&BelongSchool=%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%A1%8D%E5%A4%A9&MagicKind=%E5%85%83%E6%B0%94,%E5%86%85%E5%8A%9F')
urllist.append('https://node.jx3box.com/equip/trinket?client=std&position=5&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=5500&max_level=6430&BelongSchool=%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%A1%8D%E5%A4%A9&MagicKind=%E5%85%83%E6%B0%94,%E5%86%85%E5%8A%9F')

# urllist.append('https://node.jx3box.com/equip/weapon?client=std&position=1&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=5500&max_level=6430&BelongSchool=%E9%80%9A%E7%94%A8,%E7%B2%BE%E7%AE%80,%E8%A1%8D%E5%A4%A9&MagicKind=%E5%85%83%E6%B0%94,%E5%86%85%E5%8A%9F')
# urllist.append('https://node.jx3box.com/equip/weapon?client=std&position=0&pv_type=1&attr=&duty=2&pz=1&page=1&per=50&min_level=4500&max_level=6430&DetailType=19')
for i in urllist:
    check_zhuangbei(i)
# py()